#ifndef RAFT_H
#define RAFT_H

#include <xdevice/xdevice.h>
#include "storage.h"
#include <stdint.h>
#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

/* Raft日志条目类型 */
typedef enum {
    RAFT_LOG_WRITE = 1,
    RAFT_LOG_READ = 2,
    RAFT_LOG_SYNC = 3,
    RAFT_LOG_CONFIG = 4
} raft_log_type_t;

/* Raft日志条目 */
typedef struct {
    uint64_t term;
    uint64_t index;
    raft_log_type_t type;
    uint64_t offset;
    size_t length;
    char device_name[XDEVICE_MAX_DEVICE_NAME_LEN];
    uint64_t timestamp;
} raft_log_entry_t;

/* Raft状态 */
typedef struct {
    int node_id;
    uint64_t current_term;
    int voted_for;
    uint64_t commit_index;
    uint64_t last_applied;
    
    // 领导者状态
    uint64_t next_index[3];
    uint64_t match_index[3];
    
    storage_interface_t *storage;
};

/* Raft传输类型 */
typedef enum {
    RAFT_TRANSPORT_MEMORY = 0,
    RAFT_TRANSPORT_TCP,
    RAFT_TRANSPORT_UDP
} raft_transport_type_t;

/* Raft消息类型 */
typedef enum {
    RAFT_MESSAGE_VOTE_REQUEST = 0,
    RAFT_MESSAGE_VOTE_RESPONSE,
    RAFT_MESSAGE_APPEND_REQUEST,
    RAFT_MESSAGE_APPEND_RESPONSE
} raft_message_type_t;

/* Raft投票请求 */
typedef struct {
    uint64_t term;
    char candidate_id[XDEVICE_MAX_NODE_ID_LEN];
    uint64_t last_log_index;
    uint64_t last_log_term;
} xdevice_raft_vote_request_t;

/* Raft投票响应 */
typedef struct {
    uint64_t term;
    bool vote_granted;
} xdevice_raft_vote_response_t;

/* Raft追加请求 */
typedef struct {
    uint64_t term;
    char leader_id[XDEVICE_MAX_NODE_ID_LEN];
    uint64_t prev_log_index;
    uint64_t prev_log_term;
    uint64_t leader_commit;
    int entries_count;
    // 这里应该有entries数据，但为简化暂时省略
} xdevice_raft_append_request_t;

/* Raft追加响应 */
typedef struct {
    uint64_t term;
    bool success;
} xdevice_raft_append_response_t;

/* Raft消息结构 */
typedef struct {
    raft_message_type_t type;
    uint64_t term;
    char from_node[XDEVICE_MAX_NODE_ID_LEN];
    char to_node[XDEVICE_MAX_NODE_ID_LEN];
    
    union {
        xdevice_raft_vote_request_t vote_request;
        xdevice_raft_vote_response_t vote_response;
        xdevice_raft_append_request_t append_request;
        xdevice_raft_append_response_t append_response;
    } data;
} raft_message_t;

/* Raft回调函数 */
typedef struct {
    xdevice_error_t (*send_vote_request)(const char* node_id, 
                                        const xdevice_raft_vote_request_t* request,
                                        xdevice_raft_vote_response_t* response);
    xdevice_error_t (*send_append_entries)(const char* node_id,
                                          const xdevice_raft_append_request_t* request,
                                          xdevice_raft_append_response_t* response);
} raft_callbacks_t;

/* Raft消息结构 */
typedef struct {
    uint64_t term;
    char candidate_id[XDEVICE_MAX_NODE_ID_LEN];
    uint64_t last_log_index;
    uint64_t last_log_term;
} xdevice_raft_vote_request_t;

typedef struct {
    uint64_t term;
    bool vote_granted;
} xdevice_raft_vote_response_t;

typedef struct {
    uint64_t term;
    char leader_id[XDEVICE_MAX_NODE_ID_LEN];
    uint64_t prev_log_index;
    uint64_t prev_log_term;
    uint64_t leader_commit;
    raft_log_entry_t* entries;
    size_t entries_count;
} xdevice_raft_append_request_t;

typedef struct {
    uint64_t term;
    bool success;
    uint64_t match_index;
} xdevice_raft_append_response_t; 
/* Raft集群管理API */
int raft_cluster_init(raft_transport_type_t transport_type);
int raft_get_leader_id(void);
int raft_get_node_state(int node_id);
void raft_get_node_stats(int node_id, unsigned long *messages_processed, 
                         unsigned long *elections_started, unsigned long *heartbeats_sent);
void raft_cluster_cleanup(void);

/* 原有Raft节点API - 兼容性保留 */
int raft_node_init(raft_state_t *state, int node_id, storage_interface_t *storage);
int raft_node_append_entry(raft_state_t *state, const raft_log_entry_t *entry);
int raft_node_commit_entries(raft_state_t *state, uint64_t commit_index);
void raft_node_cleanup(raft_state_t *state);
int raft_start_event_loops(void);
void raft_communication_cleanup(void);

/* Raft传输层接口 */
typedef enum {
    RAFT_TRANSPORT_MEMORY = 0,    // 内存传输（单进程内）
    RAFT_TRANSPORT_TCP = 1,       // TCP网络传输（预留）
    RAFT_TRANSPORT_RDMA = 2       // RDMA传输（预留）
} raft_transport_type_t;

// 前向声明
struct raft_transport_interface;

/* Raft传输层API */
int raft_transport_init(raft_transport_type_t type, void *config);
int raft_transport_send(int from_node, int to_node, const raft_message_t *message);
int raft_transport_receive(int node_id, raft_message_t *message, int timeout_ms);
int raft_transport_broadcast(int from_node, const raft_message_t *message);
raft_transport_type_t raft_transport_get_type(void);
void raft_transport_cleanup(void);

/* 内存传输特定API */
void raft_memory_transport_get_stats(unsigned long *sent, unsigned long *received, unsigned long *dropped);

#ifdef __cplusplus
}
#endif

#endif // RAFT_H
